package com.zmn.plat.admin.controller.product.productgroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.interfaces.product.businessline.ProductBusinessLineBService;
import com.zmn.plat.model.entity.product.businessline.ProductBusinessLine;
import com.zmn.plat.model.vo.product.productgroup.ProductGroupVO;
import com.zmn.plat.services.interfaces.product.businessline.ProductBusinessLineService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.logger.LoggerUtil;
import com.zmn.common.utils.office.ExcelUtil;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.plat.business.interfaces.product.productgroup.ProductGroupBService;
import com.zmn.plat.common.dictionary.ProductDict;
import com.zmn.plat.model.entity.product.productgroup.ProductGroup;
import com.zmn.plat.model.entity.product.productgroup.ProductGroupQuery;
import com.zmn.plat.services.interfaces.product.productgroup.ProductGroupService;

/**
 * 产品组基础查询Controller
 *
 * @author tanghongjun
 * @since 2020-08-19 11:18:36
 */
@RestController
@RequestMapping("/product/productgroup/")
public class ProductGroupListController extends ProductGroupBaseController {

    private Logger logger = LoggerFactory.getLogger(ProductGroupListController.class);

    @Resource
    private ProductGroupService productGroupService;
    @Resource
    private ProductGroupBService productGroupBService;
    @Resource
    private ProductBusinessLineBService productBusinessLineBService;
    @Resource
    private ProductBusinessLineService productBusinessLineService;
    /**
     * 列表
     */
    @RequestMapping("list")
    @RequiresPermissions("/product/productgroup/list.action")
    public ModelAndView list(@ModelAttribute ProductGroupQuery query) {
        ModelAndView mav = new ModelAndView("product/productgroup/list");
        List<ProductGroupVO> items = productGroupService.listPageByQuery(query);
        if(NumberUtil.isPositiveInteger(query.getBizType())){
            mav.addObject("bizLineList", productBusinessLineBService.listVtByBizType(query.getBizType(),null));
        }
        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("bizTypeList", ProductDict.listBizType());
        return mav;
    }

    @RequestMapping(value = "listVtByBizType")
    public void listVtByBizType(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "parentId", defaultValue = "1") int parentId) {
        if (parentId == 0) {
            ResponseUtil.outWrite(response, "[]");
            return;
        }
        ProductGroupQuery query = new ProductGroupQuery();
        query.setBizType(parentId);
        query.setStatus(GlobalConsts.YES);
        List<ProductGroupVO> items = productGroupService.listPageByQuery(query);

        StringBuilder sb = new StringBuilder("[");
        String jsonFormat = "[\"%s\",\"%s\"]%s ";

        for (int i = 0; i < items.size(); i++) {
            ProductGroup item = items.get(i);
            String name = item.getName();
            sb.append(String.format(jsonFormat, items.get(i).getGroupId(), name, (i == items.size() - 1 ? "" : ",")));
        }
        sb.append("]");

        ResponseUtil.outWrite(response, sb.toString());
    }

    /**
     * 信息
     */
    @RequestMapping("addEditView")
    @RequiresPermissions("/product/productgroup/addEditView.action")
    public ModelAndView addEditView(@RequestParam(value = "groupId", defaultValue = "0") Integer groupId) {

        ModelAndView mav = new ModelAndView("product/productgroup/detail");

        ProductGroup productGroup;
        if (groupId == 0) {
            productGroup = new ProductGroup();
            productGroup.setGroupId(0);
        } else {
            productGroup = productGroupService.findById(groupId);
            List<VtDTO> list = productBusinessLineBService.listVtByBizType(productGroup.getBizType(),GlobalConsts.YES);

            // 如果当前的业务线被禁用，也需要加载出来
            ProductBusinessLine businessLine = productBusinessLineService.findById(productGroup.getBizLine());
            if(Objects.nonNull(businessLine)&&Objects.equals(businessLine.getStatus(),GlobalConsts.NO)){
                list.add(new VtDTO(businessLine.getRelateId(),businessLine.getName()));
            }
            mav.addObject("bizLineList", list);
        }


        mav.addObject("productGroup", productGroup);
        mav.addObject("bizTypeList", ProductDict.listBizType());


        return mav;
    }

    /**
     * 描述: 跳转设置报销金额页面
     *
     * @param groupId
     * @return org.springframework.web.servlet.ModelAndView
     * @author jiafei
     * @since 2021/9/6 18:15
     */
    @RequestMapping(value = "reimbursementAmount/addEditView")
    public ModelAndView reimbursementAmount(@RequestParam(value = "groupId", defaultValue = "0") Integer groupId) {

        ProductGroup productGroup;
        if (groupId == 0) {
            productGroup = new ProductGroup();
            productGroup.setGroupId(0);
        } else {
            productGroup = productGroupService.findById(groupId);
        }

        ModelAndView mav = new ModelAndView("product/productgroup/_reimbursement");
        mav.addObject("productGroup", productGroup);
        return mav;
    }

    /**
     * 描述: 根据条件导出产品Excel
     *
     * @param query 查询条件
     * @author wangjie
     * @since 11:43 2019/2/19
     **/
    @RequestMapping(value = "outputExcel")
    @RequiresPermissions("/product/productgroup/outputExcel.action")
    public void outputExcel(HttpServletResponse response, ProductGroupQuery query) {

        Integer pageSize = getExportPageLimit(response);
        query.setPageSize(pageSize);
        ArrayList<List> lists = productGroupBService.outputProductExcel(query);
        try {
            String name = "产品组报表.xlsx";
            name = new String(name.getBytes(), "ISO8859-1");
            ExcelUtil.export(response, lists, name, "sheet");
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "导出报表出错" + e.getMessage(), e);
        }
    }

}
